<?php
// unfortunately, we need to hide deprecated error
error_reporting(E_ALL ^ E_DEPRECATED);

// require the necessary external resource...
Blib::import('simpletest/simpletest.php');
Blib::import('simpletest/unit_tester.php');
Blib::import('simpletest/web_tester.php');
Blib::import('simpletest/reporter.php');

/**
 * unit test class
 * @author bbqq <toughgamer@gmail.com>
 * @package com.blib.core
 * @copyright bbqq qstufie.com
 */
class Blib_Unit_Test extends Blib_Class_Atomic
{
    /**
     * test suite
     * @var TestSuite
     */
    private $_test;


    /**
     * for messages...
     * @var int
     */
    static $messageCount = 0;


    /**
     * constructor
     * @param str  $label the label of test
     * @return void
     */
    public function __construct($label = 'All Tests', $dir = null)
    {
        $this->_test = new TestSuite($label);

        // add all files for test
        if (is_dir($dir)) {
            // read dir and add...
            $handler = opendir($dir);
            while ($file = readdir($handler)) {
                if (strpos($file, '.php') !== false) {
                    // add it!
                    $this->_test->addFile($dir . $file);
                }
            }

            // tidy up: close the handler
            closedir($handler);
        }

    }// end __construct


    /**
     * add file by filename
     * @param $filename
     * @return self
     */
    public function addFile($filename)
    {
        $this->_test->addFile($filename);

    }// end addFile


    /**
     * run the test...
     * @return void
     */
    public function run()
    {
        if (TextReporter::inCli()) {
            exit ($this->_test->run(new TextReporter()) ? 0 : 1);
        }

        $this->_test->run(new Blib_Unit_Reporter('UTF-8'));

    }// end run


    /**
     * add one common functions for tests: write a line of intro and
     * dump a value if necessary
     * @param str $title the title field
     * @param str $message the message to show
     * @param bool $dumpVar if true, dump it
     * @return void
     */
    public static function _M($title, $message = null, $dumpVar = true)
    {
        // counter + 1
        self::$messageCount += 1;
        $id = 'msg' . self::$messageCount;
        echo "<h4 onclick=\"if (document.getElementById('$id').style.display == 'none')" .
             " {document.getElementById('$id').style.display = 'block';} else " .
             "{document.getElementById('$id').style.display = 'none';}\" " .
             "style='padding:2px 0; border-bottom:1px solid #ccc;margin:0 0 12px 0;'><small>[show/hide]</small> $title</h4>" .
             "<div id='$id' style='display:none;'>";
        if ($dumpVar) {
            Blib::dump($message);
        } else {
            echo $message;
        }
        echo "</div>";

    }// end _M

}// end Blib_Unit_Test

// short cut
/**
 * add one common functions for tests: write a line of intro and
 * dump a value if necessary
 * @param str $title the title field
 * @param str $message the message to show
 * @param bool $dumpVar if true, dump it
 * @return void
 */
function _M($title, $message = null, $dumpVar = true)
{
    Blib_Unit_Test::_M($title, $message, $dumpVar);

}// end _M
?>
<?php
/**
 * reporter class to customise the html output
 * @author bruce.li
 *
 */
class Blib_Unit_Reporter extends HtmlReporter
{
    /**
     * (non-PHPdoc)
     * @see Centrebet/Simple/simpletest/HtmlReporter#_getCss()
     */
    function _getCss()
    {
        $css = parent::_getCss() .
               'body{font:13px lucida sans;line-height:1.8;}
               div.fail{padding:12px;background:#eee;margin:2px;color:red;}
               pre.breadcrumb{color:#eee;background:#333;padding:2px;overflow:auto;}
               div.message{margin:2px;color:#444;}';
        return $css;

    }// end _getCss


    /**
     *    Paints the test failure with a breadcrumbs
     *    trail of the nesting test suites below the
     *    top level test.
     *    @param string $message    Failure message displayed in
     *                              the context of the other tests.
     *    @access public
     */
    function paintFail($message) {
        echo '<div class="fail"><b>FAIL</b>: ';
        $breadcrumb = $this->getTestList();
        array_shift($breadcrumb);
        echo '<pre class="breadcrumb">' . implode(" -&gt; ", $breadcrumb) . '</pre>';
        echo '<div class="message">' . $this->_htmlEntities($message) . '</div></div>';
        // count...
        $this->_fails++;
    }

}// end Blib_Unit_Reporter
?>